#include "clientwindow.h"
#include "ui_clientwindow.h"
#include <QDebug>

ClientWindow::ClientWindow(QWidget *parent) :
	QMainWindow(parent), m_ui(new Ui::ClientWindow) {
	m_ui->setupUi(this);
	client = new ClientConnection(false);
	connect(client, SIGNAL(newPlayerConnected(ConnectionPlayer*)),
			m_ui->scoreTable, SLOT(addPlayer(ConnectionPlayer*)));
	connect(client, SIGNAL(playerDisconnected(qint32)), m_ui->scoreTable,
			SLOT(removePlayerById(qint32)));
	connect(client,
			SIGNAL(connectionStateChanged(QAbstractSocket::SocketState)),
			this, SLOT(onConnectionStateChanged(QAbstractSocket::SocketState)));
	connect(client,	SIGNAL(connectionStateChanged(QAbstractSocket::SocketState)),this,SIGNAL(connectionStateChanged(QAbstractSocket::SocketState)));

}

ClientWindow::~ClientWindow() {
	delete m_ui;
}

void ClientWindow::connectedControlsState()
{
	m_ui->btnConnect->setText("Disconnect from server");
	m_ui->edPort->setEnabled(false);
	m_ui->edHost->setEnabled(false);
	m_ui->edNick->setEnabled(false);
	m_ui->scoreTable->setEnabled(true);
}

void ClientWindow::disconnectedControlsState()
{
	m_ui->btnConnect->setText("Connect to server");
	m_ui->edPort->setEnabled(true);
	m_ui->edHost->setEnabled(true);
	m_ui->edNick->setEnabled(true);
	m_ui->scoreTable->setEnabled(false);
}

void ClientWindow::changeEvent(QEvent *e) {
	QMainWindow::changeEvent(e);
	switch (e->type()) {
	case QEvent::LanguageChange:
		m_ui->retranslateUi(this);
		break;
	default:
		break;
	}
}

void ClientWindow::on_btnConnect_clicked() {
	if (client->getConnectionState() == QAbstractSocket::UnconnectedState) {
		QVariant v(m_ui->edPort->text());
		bool *ok = new bool;
		int port = v.toInt(ok);
		if (*ok) {
			delete client;
			client = new ClientConnection(false);

			connect(client, SIGNAL(newPlayerConnected(ConnectionPlayer*)),m_ui->scoreTable, SLOT(addPlayer(ConnectionPlayer*)));
			connect(client, SIGNAL(playerDisconnected(qint32)),m_ui->scoreTable, SLOT(removePlayerById(qint32)));
			connect(client,	SIGNAL(connectionStateChanged(QAbstractSocket::SocketState)),this,SLOT(onConnectionStateChanged(QAbstractSocket::SocketState)));
			connect(client,	SIGNAL(connectionStateChanged(QAbstractSocket::SocketState)),this,SIGNAL(connectionStateChanged(QAbstractSocket::SocketState)));
			connect(client,	SIGNAL(newPlayerConnected(ConnectionPlayer*)),this,SIGNAL(newPlayerConnected(ConnectionPlayer*)));
			connect(client,	SIGNAL(playerDisconnected(qint32)),this,SIGNAL(playerDisconnected(qint32)));
			QHostAddress addr(m_ui->edHost->text());
			client->setAddress(addr);
			client->setPort(port);
			client->setNick(m_ui->edNick->text());
			client->connectToServer();
		}
		delete ok;
	} else {
		client->disconnectFromServer();
	}
}

void ClientWindow::onConnectionStateChanged(QAbstractSocket::SocketState state) {
	switch (state) {
		case QAbstractSocket::UnconnectedState:
			m_ui->statusbar->showMessage("Unconnected");
			m_ui->scoreTable->clearPlayers();
			disconnectedControlsState();
			break;
		case QAbstractSocket::HostLookupState:
			m_ui->statusbar->showMessage("Host Lookup...");
			break;
		case QAbstractSocket::ConnectingState:
			m_ui->statusbar->showMessage("Connecting...");
			connectedControlsState();
			break;
		case QAbstractSocket::ConnectedState:
			m_ui->statusbar->showMessage("Connected");
			m_ui->scoreTable->addPlayer(client);
			connectedControlsState();
			break;
		case QAbstractSocket::BoundState:
			m_ui->statusbar->showMessage("Bound");

			break;
		case QAbstractSocket::ClosingState:
			m_ui->statusbar->showMessage("Closing...");
			break;
		case QAbstractSocket::ListeningState:
			m_ui->statusbar->showMessage("Listening");

			break;
	}

}
